"""
参数值管理模块路由
"""
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List

from ..db import get_db
from ..schemas import (
    ParameterValue, ParameterValueCreate, ParameterValueBatch, MessageResponse
)
from ..db.parameters.crud import (
    set_parameter_value, set_parameter_values_batch, get_parameter_values,
    delete_parameter_value, get_project_by_name_en, get_parameter_category_by_name_en,
    get_parameter_by_name_en
)

router = APIRouter(
    prefix="/api/projects/{project_name_en}/categories/{category_name_en}/parameters/{param_name_en}/values",
    tags=["参数值管理"]
)


@router.post("", response_model=ParameterValue)
async def set_parameter_value_endpoint(
    project_name_en: str,
    category_name_en: str,
    param_name_en: str,
    value_data: ParameterValueCreate,
    db: Session = Depends(get_db)
):
    """设置参数值"""
    # 检查项目是否存在
    project = get_project_by_name_en(db, project_name_en)
    if not project:
        raise HTTPException(
            status_code=404,
            detail=f"项目 '{project_name_en}' 不存在"
        )
    
    # 检查分类是否存在
    category = get_parameter_category_by_name_en(db, project.id, category_name_en)
    if not category:
        raise HTTPException(
            status_code=404,
            detail=f"分类 '{category_name_en}' 不存在"
        )
    
    # 检查参数是否存在
    parameter = get_parameter_by_name_en(db, category.id, param_name_en)
    if not parameter:
        raise HTTPException(
            status_code=404,
            detail=f"参数 '{param_name_en}' 不存在"
        )
    
    return set_parameter_value(db, parameter.id, value_data.value, value_data.list_index)


@router.post("/batch", response_model=List[ParameterValue])
async def set_parameter_values_batch_endpoint(
    project_name_en: str,
    category_name_en: str,
    param_name_en: str,
    values_data: ParameterValueBatch,
    db: Session = Depends(get_db)
):
    """批量设置列表参数值"""
    # 检查项目是否存在
    project = get_project_by_name_en(db, project_name_en)
    if not project:
        raise HTTPException(
            status_code=404,
            detail=f"项目 '{project_name_en}' 不存在"
        )
    
    # 检查分类是否存在
    category = get_parameter_category_by_name_en(db, project.id, category_name_en)
    if not category:
        raise HTTPException(
            status_code=404,
            detail=f"分类 '{category_name_en}' 不存在"
        )
    
    # 检查参数是否存在
    parameter = get_parameter_by_name_en(db, category.id, param_name_en)
    if not parameter:
        raise HTTPException(
            status_code=404,
            detail=f"参数 '{param_name_en}' 不存在"
        )
    
    if not parameter.is_list:
        raise HTTPException(
            status_code=400,
            detail="只有列表参数支持批量设置值"
        )
    
    return set_parameter_values_batch(db, parameter.id, values_data.values)


@router.get("", response_model=List[ParameterValue])
async def get_parameter_values_endpoint(
    project_name_en: str,
    category_name_en: str,
    param_name_en: str,
    db: Session = Depends(get_db)
):
    """获取参数值列表"""
    # 检查项目是否存在
    project = get_project_by_name_en(db, project_name_en)
    if not project:
        raise HTTPException(
            status_code=404,
            detail=f"项目 '{project_name_en}' 不存在"
        )
    
    # 检查分类是否存在
    category = get_parameter_category_by_name_en(db, project.id, category_name_en)
    if not category:
        raise HTTPException(
            status_code=404,
            detail=f"分类 '{category_name_en}' 不存在"
        )
    
    # 检查参数是否存在
    parameter = get_parameter_by_name_en(db, category.id, param_name_en)
    if not parameter:
        raise HTTPException(
            status_code=404,
            detail=f"参数 '{param_name_en}' 不存在"
        )
    
    return get_parameter_values(db, parameter.id)


@router.delete("/{value_id}", response_model=MessageResponse)
async def delete_parameter_value_endpoint(
    project_name_en: str,
    category_name_en: str,
    param_name_en: str,
    value_id: int,
    db: Session = Depends(get_db)
):
    """删除参数值"""
    # 检查项目是否存在
    project = get_project_by_name_en(db, project_name_en)
    if not project:
        raise HTTPException(
            status_code=404,
            detail=f"项目 '{project_name_en}' 不存在"
        )
    
    # 检查分类是否存在
    category = get_parameter_category_by_name_en(db, project.id, category_name_en)
    if not category:
        raise HTTPException(
            status_code=404,
            detail=f"分类 '{category_name_en}' 不存在"
        )
    
    # 检查参数是否存在
    parameter = get_parameter_by_name_en(db, category.id, param_name_en)
    if not parameter:
        raise HTTPException(
            status_code=404,
            detail=f"参数 '{param_name_en}' 不存在"
        )
    
    success = delete_parameter_value(db, parameter.id, value_id)
    if not success:
        raise HTTPException(
            status_code=404,
            detail="参数值不存在"
        )
    
    return MessageResponse(message="参数值删除成功")

